package com.tihom.security.rbac.init;

import com.tihom.security.rbac.domain.*;
import com.tihom.security.rbac.repository.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * 默认的系统数据初始化器，永远在其他数据初始化器之前执行
 *
 * @author TiHom
 */
@Component
public class AdminDataInitializer extends AbstractDataInitializer {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    private RoleAdminRepository roleAdminRepository;

    @Autowired
    protected ResourceRepository resourceRepository;
    @Autowired
    protected RoleResourceRepository roleResourceRepository;

	@Override
	public Integer getIndex() {
		return Integer.MIN_VALUE;
	}

	@Override
	protected void doInit() {
		initResource();
		Role role = initRole();
		initAdmin(role);
	}

    /**
     * 初始化用户数据
     */
    private void initAdmin(Role role) {
        Admin admin = new Admin();
        admin.setUsername("admin");
        admin.setPassword(passwordEncoder.encode("123456"));
        adminRepository.save(admin);

        RoleAdmin roleAdmin = new RoleAdmin();
        roleAdmin.setRole(role);
        roleAdmin.setAdmin(admin);
        roleAdminRepository.save(roleAdmin);
    }

    /**
     * 初始化角色数据
     */
    private Role initRole() {
        Role role = new Role();
        role.setName("超级管理员");
        role.setCode("administrator");
        roleRepository.save(role);

        Role admin = new Role();
        admin.setName("管理员");
        admin.setCode("admin");
        roleRepository.save(admin);

        List<Resource> allRes = resourceRepository.findAll();
        for (Resource res : allRes) {
            RoleResource rr = new RoleResource();
            rr.setRole(admin);
            rr.setResource(res);
            roleResourceRepository.save(rr);
        }
        return admin;
    }

    /**
     * 初始化菜单数据
     */
    protected void initResource() {
        Resource root = createRoot("根节点");

        createResource("首页", "", "home", root);

        Resource menu1 = createResource("平台管理", "Platform", "desktop", root);

        createResource("资源管理", "resource", "", menu1);
        createResource("角色管理", "role", "", menu1);
        createResource("用户管理", "admin", "", menu1);

    }


	@Override
	protected boolean isNeedInit() {
		return adminRepository.count() == 0;
	}


	protected Resource createRoot(String name) {
		Resource node = new Resource();
		node.setName(name);
		resourceRepository.save(node);
		return node;
	}


	protected Resource createResource(String name, Resource parent) {
		return createResource(name, null, null, parent);
	}


	protected Resource createResource(String name, String link, String iconName, Resource parent) {
		Resource node = new Resource();
		node.setName(name);
		node.setIcon(iconName);
		node.setParent(parent);
		node.setType(ResourceType.MENU);
		if (StringUtils.isNotBlank(link)) {
			node.setLink(link + "Manage");
			Set<String> urls = new HashSet<>();
			urls.add(link + "Manage");
			urls.add("/" + link + "/**");
			node.setUrls(urls);
		}
		resourceRepository.save(node);
		return node;
	}
}
